home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / tftp / maincli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  112 lines

  1. /*
  2.  * tftp - Trivial File Transfer Program.  Client side.
  3.  *
  4.  * See RFC 783 for details.  Also see the "Requirements for Internet Hosts"
  5.  * RFC for additional explanations and clarifications.
  6.  */
  7.  
  8. #include    "defs.h"
  9. #include    <signal.h>
  10.  
  11. main(argc, argv)
  12. int   argc;
  13. char  **argv;
  14. {
  15.     register int    i;
  16.     register char    *s;
  17.     register FILE    *fp;
  18.  
  19.     pname = argv[0];
  20.  
  21.     while (--argc > 0 && (*++argv)[0] == '-')
  22.         for (s = argv[0]+1; *s != '\0'; s++)
  23.             switch (*s) {
  24.  
  25.             case 'h':        /* specify host name */
  26.                 if (--argc <= 0)
  27.                    err_quit("-h requires another argument");
  28.                 strcpy(hostname, *++argv);
  29.                 break;
  30.  
  31.             case 't':
  32.                 traceflag = 1;
  33.                 break;
  34.  
  35.             case 'v':
  36.                 verboseflag = 1;
  37.                 break;
  38.  
  39.             default:
  40.                 err_quit("unknown command line option: %c", *s);
  41.             }
  42.  
  43.     /*
  44.      * For each filename argument, execute the tftp commands in
  45.      * that file.  If no filename arguments were specified on the
  46.      * command line, we process the standard input by default.
  47.      */
  48.  
  49.     i = 0;
  50.     fp = stdin;
  51.     do {
  52.         if (argc > 0 && (fp = fopen(argv[i], "r")) == NULL) {
  53.             err_sys("%s: can't open %s for reading", argv[i]);
  54.         }
  55.  
  56.         mainloop(fp);        /* process a given file */
  57.  
  58.     } while (++i < argc);
  59.  
  60.     exit(0);
  61. }
  62.  
  63. mainloop(fp)
  64. FILE    *fp;
  65. {
  66.     int        sig_intr();
  67.  
  68.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  69.         signal(SIGINT, sig_intr);
  70.  
  71.     /*
  72.      * Main loop.  Read a command and execute it.
  73.      * This loop is terminated by a "quit" command, or an
  74.      * end-of-file on the command stream.
  75.      */
  76.  
  77.     if (setjmp(jmp_mainloop) < 0) {
  78.         err_ret("Timeout");
  79.     }
  80.  
  81.     if (interactive)
  82.         printf("%s", prompt);
  83.  
  84.     while (getline(fp)) {
  85.         if (gettoken(command) != NULL)
  86.             docmd(command);
  87.  
  88.         if (interactive)
  89.             printf("%s", prompt);
  90.     }
  91. }
  92.  
  93. /*
  94.  * INTR signal handler.  Just return to the main loop above.
  95.  * In case we were waiting for a read to complete, turn off any possible
  96.  * alarm clock interrupts.
  97.  *
  98.  * Note that with TFTP, if the client aborts a file transfer (such as with
  99.  * the interrupt signal), the server is not notified.  The protocol counts
  100.  * on the server eventually timing out and exiting.
  101.  */
  102.  
  103. int
  104. sig_intr()
  105. {
  106.     signal(SIGALRM, SIG_IGN);    /* first ignore the signal */
  107.     alarm(0);            /* then assure alarm is off */
  108.  
  109.     longjmp(jmp_mainloop, 1);
  110.     /* NOTREACHED */
  111. }
  112.